package com.esri.core.geodatabase;

import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.util.c;
import com.esri.core.internal.util.d;
import com.esri.core.internal.util.f;
import com.esri.core.io.UserCredentials;
import com.esri.core.tasks.geodatabase.GenerateGeodatabaseParameters;
import com.esri.core.tasks.geodatabase.SyncGeodatabaseParameters;
import com.esri.core.tasks.geodatabase.SyncModel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/* loaded from: classes.dex */
public class Geodatabase {
    private static final String a = "/GeodatabaseTempFolder";
    private static File b;
    private String c;
    private long d;
    private final AtomicBoolean e;
    private UserCredentials f;

    static native long nativeOpenShapefile(String str);

    static native void nativeSetTempDir(String str);

    native void nativeAcknowledgeUpload(long j);

    native void nativeAcknowledgeUploadByIds(long j, int[] iArr);

    native void nativeAddTable(long j, String str, String str2, int i);

    native boolean nativeCheckTableValid(long j, int i);

    native long nativeCreate(String str, String str2, String str3, int i);

    native void nativeCreateDeltaDatabase(long j, String str);

    native int[] nativeGetLayerIds(long j);

    native String[] nativeGetLayerNames(long j);

    native String[] nativeGetRasterLayerNames(String str);

    native String nativeGetServiceURL(long j);

    native String nativeGetSyncParameters(long j);

    native boolean nativeHasChanges(long j);

    native boolean nativeIsSyncEnabled(long j);

    native long nativeOpen(String str, boolean z);

    native void nativeReRegister(long j, String str, String str2);

    native boolean nativeRelease(long j);

    native void nativeSetSyncParameters(long j, String str);

    native void nativeSetUserInfo(long j, String str, String str2);

    native String nativeSyncServerDeltaDatabase(long j, String str);

    static {
        f.a().initialize();
        if (f.a().isAndroid()) {
            try {
                File a2 = c.a();
                String str = a2.getAbsolutePath() + a;
                File file = new File(str);
                b = file;
                if (file.exists()) {
                    File[] listFiles = b.listFiles();
                    if (listFiles != null) {
                        for (File file2 : listFiles) {
                            try {
                                file2.delete();
                            } catch (Exception unused) {
                            }
                        }
                    }
                } else {
                    b.mkdir();
                }
                setTempFolder(str);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static File a() {
        return b;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static Geodatabase a(String str) throws FileNotFoundException {
        File file = new File(str);
        if (!file.exists()) {
            throw new FileNotFoundException(file + " does not exist.");
        }
        return new Geodatabase(nativeOpenShapefile(str));
    }

    private Geodatabase(long j) {
        this.d = 0L;
        this.e = new AtomicBoolean(false);
        this.d = j;
    }

    public Geodatabase(String str) throws FileNotFoundException {
        this(str, false);
    }

    public Geodatabase(String str, boolean z) throws FileNotFoundException {
        this.d = 0L;
        this.e = new AtomicBoolean(false);
        File file = new File(str);
        if (!file.exists()) {
            throw new FileNotFoundException(file + " does not exist.");
        }
        this.c = file.getAbsolutePath();
        this.d = nativeOpen(str, z);
    }

    Geodatabase() {
        this.d = 0L;
        this.e = new AtomicBoolean(false);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Geodatabase(String str, String str2, String str3, SpatialReference spatialReference, UserCredentials userCredentials) {
        this.d = 0L;
        this.e = new AtomicBoolean(false);
        this.d = nativeCreate(str, str2, str3, spatialReference != null ? spatialReference.getID() : 0);
        this.c = str3;
        this.f = userCredentials;
    }

    public long getHandle() {
        return this.d;
    }

    public List<GeodatabaseFeatureTable> getGeodatabaseTables() {
        ArrayList arrayList = new ArrayList();
        for (int i : nativeGetLayerIds(this.d)) {
            arrayList.add(new GeodatabaseFeatureTable(i, this));
        }
        return arrayList;
    }

    public GeodatabaseFeatureTable getGeodatabaseFeatureTableByLayerId(int i) {
        if (nativeCheckTableValid(this.d, i)) {
            return new GeodatabaseFeatureTable(i, this);
        }
        throw new RuntimeException("Table for layer " + i + " does not exist in the geodatabase.");
    }

    public boolean hasLocalEdits() {
        return nativeHasChanges(this.d);
    }

    public SyncModel getSyncModel() throws Exception {
        SyncModel syncModel = SyncModel.GEODATABASE;
        SyncGeodatabaseParameters syncParameters = getSyncParameters();
        return syncParameters != null ? syncParameters.getSyncModel() : syncModel;
    }

    public boolean isSyncEnabled() {
        return nativeIsSyncEnabled(getHandle());
    }

    public String toString() {
        return "Geodatabase [gdbPath=" + this.c + "]";
    }

    public int hashCode() {
        String str = this.c;
        return 31 + (str == null ? 0 : str.hashCode());
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj != null && getClass() == obj.getClass()) {
            Geodatabase geodatabase = (Geodatabase) obj;
            String str = this.c;
            if (str == null) {
                if (geodatabase.c != null) {
                    return false;
                }
            } else if (!str.equals(geodatabase.c)) {
                return false;
            }
            return true;
        }
        return false;
    }

    protected void finalize() {
        d();
    }

    public void dispose() {
        d();
    }

    private void d() {
        if (this.e.compareAndSet(false, true)) {
            nativeRelease(getHandle());
            this.d = 0L;
        }
    }

    public String getPath() {
        return this.c;
    }

    public GenerateGeodatabaseParameters getRegisterParameters() throws IOException, Exception {
        return new GenerateGeodatabaseParameters(getSyncParameters().toJson(true));
    }

    public SyncGeodatabaseParameters getSyncParameters() throws Exception {
        return SyncGeodatabaseParameters.fromJson(d.c(nativeGetSyncParameters(this.d)));
    }

    public void setSyncParameters(SyncGeodatabaseParameters syncGeodatabaseParameters) throws IOException {
        nativeSetSyncParameters(this.d, syncGeodatabaseParameters.toJson(false));
    }

    public void createClientDelta(String str) {
        nativeCreateDeltaDatabase(this.d, str);
    }

    public void acknowledgeUpload(SyncGeodatabaseParameters syncGeodatabaseParameters) {
        nativeAcknowledgeUpload(this.d);
    }

    public String syncServerDelta(String str) {
        return nativeSyncServerDeltaDatabase(this.d, str);
    }

    public void reRegister(String str, String str2) {
        nativeReRegister(this.d, str, str2);
    }

    /* JADX WARN: Removed duplicated region for block: B:12:0x001e  */
    /* JADX WARN: Removed duplicated region for block: B:14:0x0022  */
    @java.lang.Deprecated
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public static void setTempFolder(java.lang.String r3) {
        /*
            if (r3 == 0) goto L1b
            int r0 = r3.length()
            if (r0 <= 0) goto L1b
            java.io.File r0 = new java.io.File
            r0.<init>(r3)
            boolean r1 = r0.isDirectory()
            if (r1 == 0) goto L1b
            boolean r0 = r0.canWrite()
            if (r0 == 0) goto L1b
            r0 = 1
            goto L1c
        L1b:
            r0 = 0
        L1c:
            if (r0 == 0) goto L22
            nativeSetTempDir(r3)
            return
        L22:
            java.lang.RuntimeException r0 = new java.lang.RuntimeException
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            r1.<init>()
            java.lang.String r2 = "The geodatabase temporary folder "
            r1.append(r2)
            r1.append(r3)
            java.lang.String r3 = " is not set."
            r1.append(r3)
            java.lang.String r3 = r1.toString()
            r0.<init>(r3)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.esri.core.geodatabase.Geodatabase.setTempFolder(java.lang.String):void");
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public List<String> b() {
        String[] nativeGetLayerNames = nativeGetLayerNames(this.d);
        return nativeGetLayerNames == null ? Collections.emptyList() : Arrays.asList(nativeGetLayerNames);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public List<String> c() {
        String[] nativeGetRasterLayerNames = nativeGetRasterLayerNames(this.c);
        return nativeGetRasterLayerNames == null ? Collections.emptyList() : Arrays.asList(nativeGetRasterLayerNames);
    }

    public String getServiceURL() {
        String nativeGetServiceURL = nativeGetServiceURL(this.d);
        return nativeGetServiceURL == null ? "" : nativeGetServiceURL;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void a(String str, String str2) {
        nativeSetUserInfo(getHandle(), str, str2);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void a(String str, String str2, SpatialReference spatialReference) {
        nativeAddTable(this.d, str, str2, spatialReference != null ? spatialReference.getID() : 0);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean a(int i) {
        int[] nativeGetLayerIds = nativeGetLayerIds(this.d);
        if (nativeGetLayerIds != null) {
            for (int i2 : nativeGetLayerIds) {
                if (i2 == i) {
                    return true;
                }
            }
            return false;
        }
        return false;
    }
}
